#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

//#include <ncurses.h>
//#include <menu.h>


#include "read_fan_temperature.h"
#include "terminal_stdscr.h"
#include "../ec_it8733_inc/ec_it8733_dev.h"

#include "../ttyrw_lib/ttyrw_lib.h"

#include "ec_it8528_rw.h"

#define DBG_UART_PRT
//#include "dbg_prt.h"
#include "../ec_it8733_inc/dbg_prt.h"

#if 0
int const_nMaxColumn()
{
	return (const_nMaxCell*3-2);
}
#endif



extern EN_RUN_STATE_TYPE en_run_state;

static void disp_menu();

void disp_menu()
{
	dbg_rn_prt( "  3     read chip id" );
	dbg_rn_prt( "  4     read_ECIO_RAM(0x0000)" );
	dbg_rn_prt( "  5     ecio_write_byte 0x3FE(01) 0x3FF(00)" );
	dbg_rn_prt( "  6     ecio_read_byte  0x3FE 0x3FF" );
	dbg_rn_prt( "  7     entry sleep status" );

	dbg_rn_prt( "  8     stop count to entry sleep status" );
	
	dbg_rn_prt( "  9     exit this program" );

}

static void *thr_sleep_fn(void *arg)
{
    //printids("new thread: ");
	int ncountsleep=0;
		
	usleep(100);
	for(ncountsleep=10;ncountsleep>0;ncountsleep--)
	{
		printf("\rwill sleep after seconds:%4d",ncountsleep);
		fflush(stdout);
		sleep(1);
		if(en_run_state!=RUN_STATE_START)
		{
			return NULL;
		}

	}

	{
		unsigned short Index=0x03FE;
		int data; //=0xAA;
		data = CEcIt8528Rw::getInstance()->ecio_read_byte(Index);
		dbg_rn_prt( "Index= 0x%04X; data: 0x%02X", Index, data );

		Index=0x03FF;
		data = CEcIt8528Rw::getInstance()->ecio_read_byte(Index);
		dbg_rn_prt( "Index= 0x%04X; data: 0x%02X", Index, data );

	}
	sleep(1);		
	//system("echo '1'|sudo -S pm-hibernate");
	//system("echo '1'|sudo -S poweroff");
	return NULL;


}

int ternimal_it8528_rw()
{	int ch;

	disp_menu();

	int err;
	pthread_t ntid;
	err = pthread_create(&ntid, NULL, thr_sleep_fn, NULL);
	if(err != 0)
	{
		dbg_rn_prt( "can't create thread: %s ", strerror(err) );
		sleep(1);
		return 0;

	}

	en_run_state=RUN_STATE_START;

	while(en_run_state!=RUN_STATE_EXIT)
    {
		
		ch = getchar();			/* If raw() hadn't been called
						 * we have to press enter before it
						 * gets to the program 		*/
		//if(ch == defKeyFMenuBgn+6 )		/* Without keypad enabled this will */
		//	printw("F7 Key pressed");/*  not get to us either	*/
						/* Without noecho() some ugly escape
						 * charachters might have been printed
						 * on screen			*/

		//dbg_rn_prt( "en_run_state: %4d; ch=%d", en_run_state, ch );

		//if(ch >= defKeyFMenuBgn  && ch <= defKeyFMenuBgn+5 )
		{
			if(ch == '3')
			{
				CEcIt8528Rw::getInstance()->read_PNPCFG(0x01);
				
				//for(int i=0;i<16;i++)
				//CEcIt8528Rw::getInstance()->ecio_read_byte(0x2000+i);
				//CEcIt8528Rw::getInstance()->pnp_read_byte(0x2000);
				
			}
			else if (ch == '4')
			{
				CEcIt8528Rw::getInstance()->read_ECIO_RAM(0x0000);

			}

			else if (ch == '5')
			{
				unsigned short Index=0x03FE;
				int data=0x01;

				CEcIt8528Rw::getInstance()->ecio_write_byte(Index,data);

				Index=0x03FF;
				data=0x00;
				CEcIt8528Rw::getInstance()->ecio_write_byte(Index,data);


			}
			
			else if (ch == '6')
			{
				unsigned short Index=0x03FE;
				int data; //=0xAA;
				data = CEcIt8528Rw::getInstance()->ecio_read_byte(Index);
				dbg_rn_prt( "Index= 0x%04X; data: 0x%02X", Index, data );

				Index=0x03FF;
				data = CEcIt8528Rw::getInstance()->ecio_read_byte(Index);
				dbg_rn_prt( "Index= 0x%04X; data: 0x%02X", Index, data );

			}	

			else if (ch == '7')
			{
				//sudo apt install hibernate
				//system("echo '1'|sudo -S hibernate-disk");

				//sudo apt install pm-utils
				//pmap pm-hibernate pm-is-supported pm-powersave pm-suspend pm-suspend-hybrid
				//system("echo '1'|sudo -S pm-hibernate");
				//system("echo '1'|sudo -S pm-suspend");
				system("echo '1'|sudo -S poweroff");
				
				#if 0
				system("echo '1'|sudo -S chmod a+w /sys/power/state");
				system("echo '1'|sudo -S echo standby > /sys/power/state");
				#endif

				#if 0
				system("echo '1'|sudo -S poweroff");
				dbg_rn_prt( "system(echo '1'|sudo -S poweroff " );
				#endif
			}

			else if (ch == '8')
			{
				en_run_state = RUN_STATE_STOP;
			}

			else if (ch == '9')
			{

				en_run_state=RUN_STATE_EXIT;
				dbg_rn_prt( "en_run_state: %4d ", en_run_state );
			}

			else if (ch == 10)  //enter
			{
				usleep(100*1000);
				disp_menu();

			}
			 

		}
	}

	return 0;
}

